//
//  XTAlertViewController.m
//

#import "XTAlertViewController.h"
#import "GXToast.h"
#import "ULNetHelper.h"
#import "Interface.h"
#import "NSString+MD5.h"



@interface XTAlertViewController ()<XTAlertDelegate,UITextFieldDelegate>
@property(nonatomic ,strong) UILabel * titleLab;
@property(nonatomic ,strong) UIView * backView;
@property(nonatomic ,strong) NSMutableArray * actionArray;
@property(nonatomic ,strong) UITextField *pwTf;
@property(nonatomic ,strong) UITextField *pwTf2;
@end

@implementation XTAlertViewController
+(instancetype)alertWithTitle:(NSString *)title andContent:(NSString *)content{

    return [[XTAlertViewController alloc] initWithTitle:title andContent:content];
}

-(instancetype)initWithTitle:(NSString *)title andContent:(NSString *)content{
    self = [super init];
    if(self){
        self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        _alertTitle = title;
        _alertContent = content;
    }
    return self;
}


-(instancetype)init{
    self = [super init];
    if(self){
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    UIImageView * backImageView = [[UIImageView alloc] init];
    backImageView.backgroundColor = [UIColor blackColor];
    backImageView.alpha = 0.5;
    [self.view addSubview:backImageView];
    backImageView.frame = self.view.frame;
    
    
    self.backView = [[UIView alloc] init];
    self.backView.backgroundColor = [UIColor whiteColor];
    self.backView.layer.cornerRadius = 8;
    self.backView.layer.masksToBounds = YES;
    [self.view addSubview:self.backView];
    self.backView.frame = CGRectMake(0, 0, 310, 287);
    self.backView.center = self.view.center;
    
    
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.font = [UIFont systemFontOfSize:15];
    self.titleLab.textAlignment = NSTextAlignmentLeft;
    [self.backView addSubview:self.titleLab];
    self.titleLab.text = @"输入新的密码";
    self.titleLab.font = [UIFont systemFontOfSize:15];
    self.titleLab.frame = CGRectMake(41, 28, self.backView.frame.size.width-82, 22.5);
    
    self.pwTf = [[UITextField alloc] initWithFrame:CGRectMake(41, 62, self.backView.frame.size.width-82, 38)];
    self.pwTf.borderStyle = UITextBorderStyleNone;
    self.pwTf.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1];
    self.pwTf.layer.cornerRadius = 4;
    self.pwTf.delegate = self;
    [self.backView addSubview:self.pwTf];
    
    UILabel *towLabel = [[UILabel alloc] init];
    towLabel.font = [UIFont systemFontOfSize:15];
    towLabel.textAlignment = NSTextAlignmentLeft;
    [self.backView addSubview:towLabel];
    towLabel.text = @"确认新的密码";
    towLabel.font = [UIFont systemFontOfSize:15];
    towLabel.frame = CGRectMake(41, 130, self.backView.frame.size.width-82, 22.5);
    
    self.pwTf2 = [[UITextField alloc] initWithFrame:CGRectMake(41, 164, self.backView.frame.size.width-82, 38)];
    self.pwTf2.borderStyle = UITextBorderStyleNone;
    self.pwTf2.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1];
    self.pwTf2.layer.cornerRadius = 4;
    self.pwTf2.delegate = self;
    [self.backView addSubview:self.pwTf2];
    
    
    UIView * line = [[UIView alloc] init];
    [self.backView addSubview:line];
    line.backgroundColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1];
    line.frame = CGRectMake(0, self.backView.frame.size.height-55.5, self.backView.frame.size.width, 0.5);
    
    UIButton *quxiaoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    quxiaoButton.frame = CGRectMake(0, self.backView.frame.size.height-55, self.backView.frame.size.width/2, 55);
    [quxiaoButton setTitleColor:[UIColor blackColor] forState:0];
    [quxiaoButton setTitle:@"取消" forState:0];
    [quxiaoButton addTarget:self action:@selector(quxiao:) forControlEvents:UIControlEventTouchUpInside];
    quxiaoButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.backView addSubview:quxiaoButton];
    
    UIButton *yesButton = [UIButton buttonWithType:UIButtonTypeCustom];
    yesButton.frame = CGRectMake(self.backView.frame.size.width/2, self.backView.frame.size.height-55, self.backView.frame.size.width/2, 55);
    [yesButton setTitleColor:[UIColor colorWithRed:107/255.0 green:132/255.0 blue:242/255.0 alpha:1] forState:0];
    [yesButton setTitle:@"确定" forState:0];
    [yesButton addTarget:self action:@selector(queding:) forControlEvents:UIControlEventTouchUpInside];
    yesButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.backView addSubview:yesButton];
    
    UIView * line2 = [[UIView alloc] init];
    [self.backView addSubview:line2];
    line2.backgroundColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1];
    line2.frame = CGRectMake(self.backView.frame.size.width/2-0.25, self.backView.frame.size.height-55, 0.5, 55);

}

- (void)quxiao:(UIButton *)button{
    [self clickItem];
}

- (void)queding:(UIButton *)button{
    button.enabled = NO;
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *uuid = [user objectForKey:@"uuid"];
    NSString *password = [user objectForKey:@"password"];
    
    NSString *time = [self nowTimeSwitchTimestamp];
    NSString *token = [[NSString stringWithFormat:@"%@-%@-1000",[self.pwTf.text MD5],time] MD5];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"studentId"] = uuid;
    dic[@"newPassword"] = [self.pwTf.text MD5];
    dic[@"oldPassword"] = [password MD5];
    dic[@"token"] = token;
    dic[@"time"] = time;
    
    [ULNetHelper POST:setPasswordPath parameters:dic success:^(id responseObject) {
        button.enabled = YES;
        NSDictionary *dic = responseObject;
        long code = [dic[@"code"] longValue];
        if (code == 200) {
            [GXToast showText:@"修改密码成功"];
            [user setValue:password forKey:@"password"];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self clickItem];
            });
        }else{
            NSString *msg = responseObject[@"msg"];
            [GXToast showText:msg];
        }
    } failure:^(NSError *error) {
        button.enabled = YES;
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}


-(NSMutableArray *)actionArray{
    if(_actionArray == nil){
        _actionArray = [[NSMutableArray alloc] init];
    }
    return _actionArray;
}


-(void)addAction:(XTAlertAction *)action{
    XTAlertViewItem * item = [[XTAlertViewItem alloc] init];
    item.text = action.title;
    item.textColor = action.titleColor;
    item.handle = action.handle;
    item.delegate = self;
    [self.actionArray addObject:item];
}

-(void)clickItem{
    if (self.block) {
        self.block();
    }
}


-(void)dealloc{
    NSLog(@"shifang");
}

- (NSString *)nowTimeSwitchTimestamp{
    NSDate *datenow = [NSDate date];//现在时间
    //这里如果long不够用,就用(long long)
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)([datenow timeIntervalSince1970]*1000)];
    return timeSp;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


#pragma mark ============弹窗Action============
@implementation XTAlertAction
+(instancetype)actionWithTitle:(NSString *)title andTitleColor:(UIColor *)titleColor action:(XTAlertActionBlock)handel{
    return [[XTAlertAction alloc] initWithTitle:title andTitleColor:titleColor action:handel];
}

-(instancetype)initWithTitle:(NSString *)title andTitleColor:(UIColor *)titleColor action:(XTAlertActionBlock)handel{
    self = [super init];
    if(self){
        _title = title;
        _titleColor = titleColor;
        _handle = handel;
    }
    return self;
}


@end
#pragma mark ============弹窗Item============
@implementation XTAlertViewItem
-(instancetype)init{
    self = [super init];
    if(self){
        self.textAlignment = NSTextAlignmentCenter;
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAction)]];
    }
    return self;
}


-(void)clickAction{
    [self.delegate clickItem];
    if(self.handle){
        self.handle();
    }
}

@end
